package com.beordie.controller;

import com.beordie.model.*;
import com.beordie.mvc.ResponseText;
import com.beordie.service.IDriverService;
import com.beordie.service.IPoliceService;
import com.beordie.service.impl.DriverServiceImpl;
import com.beordie.service.impl.PoliceServiceImpl;
import com.beordie.utils.DateFormatUtils;
import com.beordie.utils.JsonUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @Classname CarController
 * @Description
 * @Date 2021/7/11 21:24
 * @Created 30500
 */
public class PoliceController {
    IPoliceService policeService = new PoliceServiceImpl();

    @ResponseText("/police/getAll.udo")
    public String getAllCars(HttpServletRequest request, HttpServletResponse response) {
        //1.    获取查询数据的起始索引值
        String offsetS = request.getParameter("offset") == null ? "0" : request.getParameter("offset");
        int offset = Integer.parseInt(offsetS);

        //2.    获取当前页要查询的数据量
        String pageNumberS = request.getParameter("pageNumber") == null ? "5" : request.getParameter("pageNumber");
        int pageNumber = Integer.parseInt(pageNumberS);

        List<Police> result = policeService.getAllByLimit(true, offset, pageNumber);
        int total = policeService.getCount();

        //4.    将集合封装为 bootstrap-table识别的格式
        ResultData<Police> resultData = new ResultData<>();
        resultData.setRows(result);
        resultData.setTotal(total);
        return JsonUtils.parseObject(resultData);
    }

    @ResponseText("/police/insert.udo")
    public String insert(HttpServletRequest request, HttpServletResponse response) {
        String numberPolice = request.getParameter("numberPolice");
        String name = request.getParameter("name");
        String number = request.getParameter("number");
        int sex = Integer.parseInt(request.getParameter("sex"));
        String address = request.getParameter("address");
        String phone = request.getParameter("phone");

        Police police = new Police(0, numberPolice, name, number, sex, address, phone);
        System.out.println(police);
        int result = policeService.insert(police);
        System.out.println(result);
        Message message = new Message();
        if (result > 0) {
            message.setStatus(0);
            message.setResult("新增成功");
        } else {
            message.setStatus(-1);
            message.setResult("新增错误");
        }
        return JsonUtils.parseObject(message);
    }

    @ResponseText("/police/findByNumber.udo")
    public String findByNumber(HttpServletRequest request, HttpServletResponse response) {
        String number = request.getParameter("number");
        Police driver = policeService.findByNumName(number);

        Message message = new Message();
        if (driver != null) {
            message.setStatus(0);
            message.setResult("查询成功");
            message.setData(driver);
        } else {
            message.setStatus(-1);
            message.setResult("查询失败");
        }
        return JsonUtils.parseObject(message);
    }

    @ResponseText("/police/delete.udo")
    public String deleteById(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        Message message = new Message();
        int result = policeService.deleteById(id);
        if (result > 0) {
            message.setStatus(0);
            message.setResult("删除成功");
        } else {
            message.setStatus(-1);
            message.setResult("删除失败");
        }
        return JsonUtils.parseObject(message);
    }

    @ResponseText("/police/update.udo")
    public String updateById(HttpServletRequest request, HttpServletResponse response) {
        String numberPolice = request.getParameter("numberPolice");
        String name = request.getParameter("name");
        String number = request.getParameter("number");
        int sex = Integer.parseInt(request.getParameter("sex"));
        String address = request.getParameter("address");
        String phone = request.getParameter("phone");
        int id = Integer.parseInt(request.getParameter("id"));

        Police police = new Police(0, numberPolice, name, number, sex, address, phone);
        Message message = new Message();
        int result = policeService.updateById(police, id);
        if (result > 0) {
            message.setStatus(0);
            message.setResult("更新成功");
        } else {
            message.setStatus(-1);
            message.setResult("更新失败");
        }
        return JsonUtils.parseObject(message);
    }
}
